﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;

namespace Manage_Pupils
{
    public partial class FormQuiDinh : Form
    {
        private SqlConnection con;
        private DataTable dtQD = new DataTable("tbQuyDinh");//Mo phong cac Table trong CSDL
        private DataTable dtXL = new DataTable("tbXepLoai");
        private SqlDataAdapter daQD = new SqlDataAdapter();  //Quan ly cac ket noi den nguon du lieu
        private SqlDataAdapter daXL;

        //================================================================================================

        //Ket noi co so du lieu
        private void connect()
        {
            try
            {
                con = new SqlConnection("Server=IWANTWIN-PC; user id=sa; password=123456; database=QuanLyHocSinh");
                con.Open();

            }
            catch
            {
                MessageBox.Show("Ban da ket noi that bai", "Loi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        //=================================================================================================

        //Giai phong bo nho
        private void disconnect()
        {
            con.Close();
            con.Dispose();
            con = null;

        }

        //==================================================================================================

        //lay ra cac thong tin can thiet tu bang Quy Dinh de gan vao bang tbQuiDinh de gan vao datatabble 
        private void getdata()
        {

            SqlCommand command = new SqlCommand();
            command.Connection = con;
            command.CommandType = CommandType.Text;


            //Ket noi
            /*
            DataTable dtXepLoai = new DataTable();
            command.CommandText = "Select * TenLoai from XepLoai";
            daQD.SelectCommand = command;
            dtQD.Clear();
            daQD.Fill(dtXepLoai);

            cbXepLoai.DataSource = dtXepLoai;
            cbXepLoai.DisplayMember = "TenLoai";
            cbXepLoai.ValueMember = "TenLoai";
            cbXepLoai.SelectedValue = "TenLoai";
            */
            //Xac dinh loai cau lenh truy van du lieu,Text: cau lenh SQL(ngam dinh)
            dtQD.Clear();
            command.CommandText = @"Select 
                                    TuoiToiThieu ,
                                    TuoiToiDa ,
                                    SoLuongHSToiDa ,
                                    SoLop ,
                                    DiemDat,
                                    SoLuongMonHoc
                                    from QuiDinh";

            daQD.SelectCommand = command;  //Gan command cho da
            daQD.Fill(dtQD);  //Nap du lieu cho table
            //grvQD.DataSource = dtQD;//Load du lieu len DataGriview
            tbox_TuoiMin.Text = dtQD.Rows[0][0].ToString();
            tbox_SoLuongMonHoc.Text = dtQD.Rows[0][5].ToString();
            tbox_SoHSToiDa.Text = dtQD.Rows[0][2].ToString();
            tbox_TuoiMax.Text = dtQD.Rows[0][1].ToString();
            tbox_SoLopToiDa.Text = dtQD.Rows[0][3].ToString();
            tbox_QuiDinh.Text = dtQD.Rows[0][4].ToString();

        }

        //========================================================================
        public FormQuiDinh()
        {
            //InitializeComponent();
            InitializeComponent();
        }

        private void button_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void FormQuiDinh_Load(object sender, EventArgs e)
        {
            connect();
            //getdata();
            // disconnect();


            string sqlSelect = "Select * from Xeploai";
            // ORDER BY ASC";

            if (con == null) con = new SqlConnection("Server=IWANTWIN-PC; user id=sa; password=123456; database=QuanLyHocSinh");
            getdata();
            SqlCommand cmdSelect = new SqlCommand("Select * from XepLoai", con);
            daXL = new SqlDataAdapter(cmdSelect);
            SqlCommandBuilder cb = new SqlCommandBuilder(daXL);
            //daXL.SelectCommand = cb.get();
            daXL.InsertCommand = cb.GetInsertCommand();
            daXL.UpdateCommand = cb.GetUpdateCommand();

            if (dtXL != null) dtXL.Clear();
            daXL.Fill(dtXL);

            cbXepLoai.DataSource = dtXL;
            cbXepLoai.DisplayMember = "TenLoai";
            //cbXepLoai.ValueMember = "TenLoai";
            //cbXepLoai.SelectedValue = "TenLoai";
            tbox_DiemMin.Clear();
            tbox_DiemMin.DataBindings.Add("Text", dtXL, "TuoiMin");
            tbox_DiemMax.Clear();
            tbox_DiemMax.DataBindings.Add("Text", dtXL, "TuoiMax");

            // tbox_tuoiMin. = false;
        }

        private void button_Save_Click(object sender, EventArgs e)
        {
            int tuoimin, tuoimax, SLuongHS, Solop, DiemDat, SoluongMonHoc;

            try
            {
                tuoimin = int.Parse(tbox_TuoiMin.Text);
                tuoimax = int.Parse(tbox_TuoiMax.Text);
                SLuongHS = int.Parse(tbox_SoHSToiDa.Text);
                Solop = int.Parse(tbox_SoLopToiDa.Text);
                DiemDat = int.Parse(tbox_QuiDinh.Text);
                SoluongMonHoc = int.Parse(tbox_SoLuongMonHoc.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi nhập số liệu", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;

            }


            SqlCommand commandUpdate = new SqlCommand();
            commandUpdate.Connection = con;
            commandUpdate.CommandText = @"Update  QuiDinh SET TuoiToiThieu=" + tuoimin + "," +
                                                         " TuoiToiDa=" + tuoimax + "," +
                                                         " SoLuongHSToiDa=" + SLuongHS + "," +
                                                         " SoLop=" + Solop + "," +
                                                         " DiemDat=" + DiemDat + "," +
                                                         " SoLuongMonHoc=" + SoluongMonHoc;

            try
            {
                if (commandUpdate.ExecuteNonQuery() > 0)
                {
                    MessageBox.Show("Thay đổi qui định thành công", "Successful", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
            }
            catch (Exception)
            {
                MessageBox.Show("Không cập nhập được thông tin", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }


            try
            {
                BindingContext[dtXL].EndCurrentEdit();
                if (daXL.Update(dtXL) > 0)
                    MessageBox.Show("Thay đổi đánh giá học lực thành công", "Successful", MessageBoxButtons.OK, MessageBoxIcon.Information);
                else
                    MessageBox.Show("Không thay đổi được", "Successful", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi cập nhập đánh giá qui định", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        //=================================================================================================================
    }
}
